package mmc.stats;

import java.io.IOException;
import java.io.Serializable;

import mmc.MovableEngineObject;


@SuppressWarnings("serial")
public class Stats implements Serializable{
	
	//@Nermin - serializing lists is really bad for networking -> rewritten using arrays
	//public List<Stat> stats = new ArrayList<Stat>();
	Stat[] stats;
	
	//define stats
	
	//read stats
	
	//set stats
	
	//applicator 
	
	public Stats(MovableEngineObject object){			//typeof Player
		if(object instanceof mmc.server.Player){
			stats = new Stat[]{
					new Stat(StandardStats.HP, 1, 100), 
					new Stat(StandardStats.EN, 1, 100), 
					new Stat(StandardStats.STR, 1, 10)};
		}else{
			stats = new Stat[]{new Stat(StandardStats.HP, 1, 100)};
		}
	}
	
	public int getValue(String name){
		for(Stat s : stats){
			if(s.getName().equals(name)){
				return s.getActualValue();
			}
		}
		
		return -1;
	}
	
	public float getPercentalValue(String name){
		for(Stat s : stats){
			if(s.getName().equals(name)){
				return (float)s.getActualValue()/s.getMaximum();
			}
		}
		
		return -1;
	}
	
	public void modifyActualValue(String name, int absolute, int percental){
		for(Stat s : stats){
			if(s.getName().equals(name)){
				synchronized (s) {
					s.add(absolute, percental);	
				}				
				break;
			}
		}			
	}
	
	public void modifyMaximumValue(String name, float percental){
		for(Stat s : stats){
			if(s.getName().equals(name)){
				synchronized (s) {
					s.affect(percental);	
				}				
				break;
			}
		}
	}
	
	private void writeObject(java.io.ObjectOutputStream out) throws IOException{
		out.writeInt(stats.length);
		for(Stat s : stats)
			out.writeUnshared(s);
	}
 
	private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException{
		stats = new Stat[in.readInt()];
		
		for(int i=0;i<stats.length;i++)
			stats[i] = (Stat) in.readObject();
	}
}
